package com.library.service.impl;

import com.library.common.exception.base.BaseException;
import com.library.common.utils.DateUtils;
import com.library.common.utils.SecurityUtils;
import com.library.domain.AppActivity;
import com.library.domain.AppJoinActivity;
import com.library.mapper.AppActivityMapper;
import com.library.mapper.AppJoinActivityMapper;
import com.library.service.IAppActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author julyWhj
 * @Description $
 * @Date 2022/3/25 4:57 下午
 **/
@Service
public class AppActivityServiceImpl implements IAppActivityService {
    @Resource
    private AppActivityMapper appActivityMapper;
    @Resource
    private AppJoinActivityMapper appJoinActivityMapper;

    /**
     * 查询活动管理
     *
     * @param activityId 活动管理主键
     * @return 活动管理
     */
    @Override
    public AppActivity selectAppActivityByActivityId(Long activityId) {
        return appActivityMapper.selectAppActivityByActivityId(activityId);
    }

    @Override
    public AppActivity selectAppActivityByActivityNo(String activityNo) {
        return appActivityMapper.selectAppActivityByActivityNo(activityNo);
    }

    /**
     * 查询活动管理列表
     *
     * @param appActivity 活动管理
     * @return 活动管理
     */
    @Override
    public List<AppActivity> selectAppActivityList(AppActivity appActivity) {
        return appActivityMapper.selectAppActivityList(appActivity);
    }

    /**
     * 新增活动管理
     *
     * @param appActivity 活动管理
     * @return 结果
     */
    @Override
    public int insertAppActivity(AppActivity appActivity) {
        appActivity.setCreateTime(DateUtils.getNowDate());
        appActivity.setUserId(SecurityUtils.getUsername());
        appActivity.setDeptId(SecurityUtils.getDeptId());
        return appActivityMapper.insertAppActivity(appActivity);
    }

    /**
     * 修改活动管理
     *
     * @param appActivity 活动管理
     * @return 结果
     */
    @Override
    public int updateAppActivity(AppActivity appActivity) {
        appActivity.setUpdateTime(DateUtils.getNowDate());
        return appActivityMapper.updateAppActivity(appActivity);
    }

    /**
     * 批量删除活动管理
     *
     * @param activityIds 需要删除的活动管理主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityByActivityIds(Long[] activityIds) {
        return appActivityMapper.deleteAppActivityByActivityIds(activityIds);
    }

    /**
     * 删除活动管理信息
     *
     * @param activityId 活动管理主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityByActivityId(Long activityId) {
        return appActivityMapper.deleteAppActivityByActivityId(activityId);
    }

    @Override
    public void signUpActivity(String activityNo) {
        Long userId = SecurityUtils.getUserId();
        AppActivity appActivity = appActivityMapper.selectAppActivityByActivityNo(activityNo);
        AppJoinActivity appJoinActivity = new AppJoinActivity();
        appJoinActivity.setActivityId(appActivity.getActivityId());
        appJoinActivity.setMemberId(userId);
        AppJoinActivity dbAppJoinActivity = appJoinActivityMapper.selectOneByActivityIdAndMemberId(appJoinActivity);
        if (Objects.isNull(dbAppJoinActivity)) {
            dbAppJoinActivity = new AppJoinActivity();
            dbAppJoinActivity.setActivityId(appActivity.getActivityId());
            dbAppJoinActivity.setMemberId(userId);
            dbAppJoinActivity.setStatus(1L);
            appJoinActivityMapper.insertAppJoinActivity(dbAppJoinActivity);
        } else {
            throw new BaseException("用户已报名无须重复报名");
        }
    }

    /**
     * 扫码签到
     *
     * @param activityNo 活动编码
     */
    @Override
    public void sweepTheCodeSign(String activityNo) {
        Long userId = SecurityUtils.getUserId();
        AppActivity appActivity = appActivityMapper.selectAppActivityByActivityNo(activityNo);
        AppJoinActivity appJoinActivity = new AppJoinActivity();
        appJoinActivity.setActivityId(appActivity.getActivityId());
        appJoinActivity.setMemberId(userId);
        AppJoinActivity dbAppJoinActivity = appJoinActivityMapper.selectOneByActivityIdAndMemberId(appJoinActivity);
        if (Objects.isNull(dbAppJoinActivity)) {
            dbAppJoinActivity = new AppJoinActivity();
            dbAppJoinActivity.setActivityId(appActivity.getActivityId());
            dbAppJoinActivity.setMemberId(userId);
            dbAppJoinActivity.setStatus(2L);
            appJoinActivityMapper.insertAppJoinActivity(dbAppJoinActivity);
        } else {
            dbAppJoinActivity.setStatus(2L);
            appJoinActivityMapper.updateAppJoinActivity(dbAppJoinActivity);
        }
    }
}
